package com.nlelpct.manage.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nlelpct.manage.constant.SysAuthenticationConstant;
import com.nlelpct.manage.constant.SysSolrLogConstant;
import com.nlelpct.manage.constant.SystemConstant;
import com.nlelpct.manage.context.ThreadParameter;
import com.nlelpct.manage.enums.SysAuthenticationTypeEnum;
import com.nlelpct.manage.feign.EsAuthenticationFeign;
import com.nlelpct.manage.mapper.SysCompanyMapper;
import com.nlelpct.manage.mapper.SysEmployeeMapper;
import com.nlelpct.manage.model.query.AuthenticationLogQueryVo;
import com.nlelpct.manage.model.query.TerminalInfo;
import com.nlelpct.manage.model.vo.EsAuthenticationLog;
import com.nlelpct.manage.utils.GetLoginInfoUtils;
import com.nlelpct.manage.utils.IDUtils;
import com.nlelpct.manage.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Author 刘伟涛
 * @Date 2023/10/26 17:32
 **/
@Service
@Slf4j
public class EsAuthenticationLogService {
    private final EsAuthenticationFeign esAuthenticationFeign;

    private final SysEmployeeMapper sysEmployeeMapper;

    private final RedisTemplate redisTemplate;

    private final SysCompanyMapper sysCompanyMapper;


    @Autowired
    public EsAuthenticationLogService(EsAuthenticationFeign esAuthenticationFeign, SysEmployeeMapper sysEmployeeMapper, RedisTemplate redisTemplate, SysCompanyMapper sysCompanyMapper) {
        this.esAuthenticationFeign = esAuthenticationFeign;
        this.sysEmployeeMapper = sysEmployeeMapper;
        this.redisTemplate = redisTemplate;
        this.sysCompanyMapper = sysCompanyMapper;
    }

    public Result saveLog(Integer authenticationTypeId,String authenticationWay, Map<String, String> map){
        EsAuthenticationLog esAuthenticationLog = new EsAuthenticationLog();
        String systemId = this.getSystemId();
        esAuthenticationLog.setSystemId(systemId);
        esAuthenticationLog.setCompanyId(this.getCurrentCompanyId());
        esAuthenticationLog.setOperatorId(this.getCurrentEmployeeId());
        esAuthenticationLog.setOperateTime(LocalDateTime.now().toString());
        esAuthenticationLog.setOperateTimeLong(System.currentTimeMillis());
        esAuthenticationLog.setAuthenticationTypeId(authenticationTypeId);
        esAuthenticationLog.setAuthenticationWay(authenticationWay);

        //操作人名称
        map.put(SysAuthenticationConstant.OPERATE_NAME, sysEmployeeMapper.selectById(this.getCurrentEmployeeId()).getEmployeeName());
        //企业名称
        map.put(SysAuthenticationConstant.COMPANY_NAME, sysCompanyMapper.selectById(this.getCurrentCompanyId()).getCompanySysName());
        //操作类型
        map.put(SysAuthenticationConstant.AUTHENTICATION_TYPE, SysAuthenticationTypeEnum.getName(authenticationTypeId));
        TerminalInfo terminalInfo = this.getTerminalInfo();
        //ip地址
        map.put(SysAuthenticationConstant.IP_ADDRESS, terminalInfo.getIpAddress());
        //ip所属地区
        map.put(SysAuthenticationConstant.IP_AREA, terminalInfo.getIpArea());
        //操作系统
        map.put(SysAuthenticationConstant.OS, terminalInfo.getOs());
        //浏览器名称和内核
        map.put(SysAuthenticationConstant.BROWSE, terminalInfo.getBrowser());


        esAuthenticationLog.setContent(JSONObject.toJSONString(map));
        return esAuthenticationFeign.saveLog(esAuthenticationLog);
    }

    /**
     * 获取当前系统ID
     *
     * @return 系统ID
     */
    public String getSystemId() {
        return GetLoginInfoUtils.getSystemId();
    }

    /**
     * 获取员工ID
     * @return 员工ID
     */
    public String getCurrentEmployeeId() {
        return GetLoginInfoUtils.getEmployeeId();
    }

    /**
     * 获取企业ID
     * @return 企业ID
     */
    public String getCurrentCompanyId() {
        return GetLoginInfoUtils.getCompanyId();
    }

    /**
     * 获取IP等信息
     */
    public TerminalInfo getTerminalInfo() {
        String jsonTerminalInfo = ThreadParameter.getLocalMap().get(SystemConstant.TERMINAL_INFO);
        return JSON.parseObject((String) redisTemplate.opsForValue().get(jsonTerminalInfo), TerminalInfo.class);
    }
}
